<?php require "../global.php";?>
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="/Public/jsLibrary/Jslibary.js"></script>
<style>
body,td{font-size:12px;}
.spanplace{display:none;}
.xztdbgc{background-color:#FFDD99;}
</style>

<?php
require "Uifunction.php";
$thismonth=$_REQUEST["thismonth"];//本月值

//规定值格式
/*
存放后值如下:
时间值(2012-09-04)|市场价|梦之旅|房差价|库存量,时间值2(2012-09-05)|市场价2|梦之旅2|房差价2|库存量2,............
*/
$datecontent="2012-09-11|500|400|200|10,2012-09-17|520|410|230|20";
if($thismonth=="") $thismonth=date("Y-m");
$thefirstdayofweek=date('w',strtotime($thismonth."-01"));//本月第一天是星期几

//如果这个月的第一天不是星期一,那么，它的前面就以上一个月的补齐
//获取前上一个月的日期,以这个月的第一天为基准
function getqiandate($datestr,$num){
	return date("Y-m-d",strtotime($datestr)-$num*3600*24);
}

//获取前一个月的日期，返回值是2012-09
function getqimonth($thismonth){
	$yearn=preg_replace("/\-[\d]{1,2}$/","",$thismonth);
	$month=preg_replace("/^[\d]{4}\-[0]*/","",$thismonth);
	
	$nowmonth=($month==1)?12:$month-1;
	if($month==1){
		$nowmonth=12;
		$yearn=$yearn-1;
	}
	else{
		$nowmonth=$month-1;
	}
	return $yearn."-".substr("0".$nowmonth,-2);
}

//获取后一个月的日期
function getnextmonth($thismonth){
	$yearn=preg_replace("/\-[\d]{1,2}$/","",$thismonth);
	$month=preg_replace("/^[\d]{4}\-[0]*/","",$thismonth);
	if($month==12){
		$month=1;
		$yearn+=1;
	}
	else{
		$month+=1;
	}
	return $yearn."-".substr("0".$month,-2);
	
}

$qianbunum=$thefirstdayofweek==0?6:$thefirstdayofweek-1;//前面要补的天数
$monthmaxnum=date("t",strtotime($thismonth."-01"));//这个月有多少天

$lastbunum=7-($monthmaxnum+$qianbunum) % 7;
$lastbunum=$lastbunum==7?0:$lastbunum;//获取后面需要被的天数


//计算这次要循环的次数
$maxfornum=$qianbunum+$lastbunum+$monthmaxnum;

//获取本次的开始天数
$stardate= getqiandate($thismonth,$qianbunum);


?>
批量设置<input type="checkbox" id="piliang">
<div>
<div style="float:left; width:510px;">
<table width="500" border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" style="border-collapse:collapse">
  <tr>
    <td height="37" colspan="7" align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="37%" height="27"><a href="?thismonth=<?php $pmonth= getqimonth($thismonth);echo $pmonth; ?>"><?php echo preg_replace("/^[\d]{4}\-[0]*/","",$pmonth);?>月</a></td>
        <td width="27%"><strong><?php echo str_replace("-","年",$thismonth)."月";?></strong><span class="spanplace" id="placeall">全选<input type="checkbox" id="cpall"></span></td>
        <td width="36%" align="right"><a href="?thismonth=<?php $nmonth= getnextmonth($thismonth);echo $nmonth;?>"><?php echo preg_replace("/^[\d]{4}\-[0]*/","",$nmonth);?>月</a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td width="11%" height="37" align="center">一<span class="spanplace" id="place1"><input type="checkbox" id="cpz1"></span></td>
    <td width="14%" align="center">二<span class="spanplace" id="place2"><input type="checkbox" id="cpz2"></span></td>
    <td width="13%" align="center">三<span class="spanplace" id="place3"><input type="checkbox" id="cpz3"></span></td>
    <td width="13%" align="center">四<span class="spanplace" id="place4"><input type="checkbox" id="cpz4"></span></td>
    <td width="15%"  align="center">五<span class="spanplace" id="place5"><input type="checkbox" id="cpz5"></span></td>
    <td width="15%" align="center">六<span class="spanplace" id="place6"><input type="checkbox" id="cpz6"></span></td>
    <td width="19%" align="center">日<span class="spanplace" id="place0"><input type="checkbox" id="cpz0"></span></td>
  </tr>
 <?php
 $zcthismonth=turntostrtozc($thismonth);
 preg_match_all("/".$zcthismonth."\-[\d]{1,2}\|[\d\.]+\|[\d\.]+\|[\d]*\|[\d]+/",$datecontent,$datecontentarr);//只获取本月的日期值
 $dcontentarr=array();//存放本月分解的数值
 foreach($datecontentarr[0] as $k=>$v){
	 $varr=explode("|",$v);
	$dcontentarr[$varr[0]][1]=$varr[1];
	$dcontentarr[$varr[0]][2]=$varr[2];
	 $dcontentarr[$varr[0]][3]=$varr[3];
	 $dcontentarr[$varr[0]][4]=$varr[4]; 
 }

 
for($i=1;$i<=$maxfornum;$i++){
	$thisv=date("Y-m-d",strtotime($stardate)+($i-1)*3600*24);
 if($i %7 ==1){
?>  <tr><?php }?>
    <td width="11%" align="left" valign="top" height="50"  
    <?php if(strstr($thisv,$thismonth)=="" or date("Y-m")>$thismonth){
		echo "style='background-color:#E6DFDF'";}
		else{
			echo "data='".$thisv."|".$dcontentarr[$thisv][1]."|".$dcontentarr[$thisv][2]."|".$dcontentarr[$thisv][3]."|".$dcontentarr[$thisv][4]."' week='".date("w",strtotime($thisv))."'";
		}
		?>
        class="">
		<div style="width:100%;height:20px;">
        	<div style="float:left;"><span style="margin-top:15px;  margin-left:5px; color: #396; font-size:14px;"><?php echo preg_replace("/^[\d]{4}\-[\d]{1,2}\-[0]*/","",$thisv);?></span>
        	</div>
            <?php if($dcontentarr[$thisv]){?>
             <div style="float:right;"><span style="color:333; margin-right:5px;">0/<?php echo $dcontentarr[$thisv][4];?></span></div>
             <?php }?>
             
        </div>
        <?php if($dcontentarr[$thisv]){?>
		<div style="width:100%;">
            <span style="color:#999">市:￥<?php echo $dcontentarr[$thisv][1];?></span><br>
            <span>梦:￥<?php echo $dcontentarr[$thisv][2];?></span>
        </div>
        <?php }?>
    </td>
 <?php if($i % 7==0){?> </tr><?php }?>
 <?php
}
 ?> 
</table>

</div>
<div style="float:left; width:200px; overflow:hidden;">
<table width="94%"  border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" style="border-collapse:collapse">
  <form action="?" method="post" name="formsubmit">
 <input type="hidden" id="checkeddatev" name="checkeddatev">
  <tr>
    <td height="28" colspan="2" align="center">价格/库存设置</td>
    </tr>
     <tr>
       <td height="29" align="right" >市 场 价:</td>
       <td >
        <input type="text" name="market_price" id="market_price"  size="12"/>
        <span style="color:#F00">*</span></td>
     </tr>
     <tr>
       <td height="34" align="right" >梦之旅价:</td>
       <td ><input type="text" name="mzl_price" id="mzl_price"  size="12"/>
         <span style="color:#F00">*</span></td>
     </tr>
     <tr>
       <td height="27" align="right" >房 差:</td>
       <td ><input type="text" name="fangcha" id="fangcha"  size="12"/></td>
     </tr>
     <tr>
       <td height="32" align="right" >库 存:</td>
       <td ><input type="text" name="kucun" id="kucun"  size="12"/>
         <span style="color:#F00">*</span></td>
     </tr>
     <tr>
       <td height="30" >&nbsp;</td>
       <td ><input type="button" name="mysubmit" id="mysubmit" value="设 置" /> <input type="button" name="mysreset" id="mysreset" value="重 置" /></td>
     </tr>
     <tr>
   
    </tr>
    </form>
</table>

</div>
</div>

<script language="javascript">
$(document).ready(function(){
	//绑定批量设置的事件
	$("#piliang").bind("click", function(){
		if($("#piliang").attr("checked")=="checked"){
			$("span[id^='place']").show();
		}else{
			$("span[id^='place']").hide();	
		}
	});
	
	//绑定全选事件的功能
	$("#cpall").bind("click",function(){
		if($("#cpall").attr("checked")=="checked"){
			$("input[id^='cpz']").attr("checked","checked");
			$("td[week]").addClass("xztdbgc");
		}else{
			$("input[id^='cpz']").removeAttr("checked");
			$("td[week]").removeClass();
		}
	});
	
	//给选定每周的一天绑定事件
	$("input[id^='cpz']").bind("click", function(){
		id=$(this).attr("id");
		id=id.replace("cpz","");
		if($(this).attr("checked")=="checked"){
			
			$("td[week^='"+id+"']").addClass("xztdbgc");
			$("td[week^='"+id+"']").each(function(){
				adddatetoseleed(this);
			});	
		}
		else{
		$("td[week^='"+id+"']").removeClass();
			$("td[week^='"+id+"']").each(function(){
				deleteofseleed(this);
			});	
		}
	});
	
	//给每一天绑定事件
	$("td[data]").bind("click",function(){
		classname=$(this).attr("class");
		if(classname=="xztdbgc"){
			$(this).removeClass();
			 deleteofseleed(this);
		}
		else{
			$(this).addClass("xztdbgc");
			adddatetoseleed(this);
		}
		
		//如果本月选定的只有一天时，在填写价格处写上这一天的价格
		chooesv=$("#checkeddatev").val();
		if(chooesv.match(/^,[\d]{4}\-[\d]{1,2}\-[\d]{1,2}$/)){
				
				chooesv=chooesv.replace(",","");
				
				$("td[data^='"+chooesv+"']").each(function(){
					data=$(this).attr("data");
					if(data!=""){
						dataarr=data.split("|");
						if(dataarr.length==5){
							$("#market_price").val(dataarr[1]);
							$("#mzl_price").val(dataarr[2]);
							$("#fangcha").val(dataarr[3]);
							$("#kucun").val(dataarr[4]);
						}
					}
					
				});
		}
		else{
			$("#market_price").val("");
			$("#mzl_price").val("");
			$("#fangcha").val("");
			$("#kucun").val("");	
		}
	});
	
	
	//绑定设置价格按钮功能
	$("#mysubmit").bind("click",function(){
		if(!nzeletype("market_price","请输入市场价","float")) return false;
		if(!nzeletype("mzl_price","请输入梦之旅价","float")) return false;
		
		if($("#mzl_price").val()>$("#market_price").val()){
			alert("梦之旅价不能大于市场价");
				$("#mzl_price").val("");
				$("#mzl_price").focus();
				return false;
		}
		if(!nzeletype("kucun","请输入库存数量","int")) return false;
		
	});
});

//给当前值加入到已经选择到的值里
function adddatetoseleed(strdate){
	data=$(strdate).attr("data");
	
	data=data.match(/^[\d]{4}\-[\d]{1,2}\-[\d]{1,2}/);
	tv=$("#checkeddatev").val();
	if(tv.indexOf(data)<1)	$("#checkeddatev").val(tv+","+data);
}
//给当前值从已经选择到的值里删除
function deleteofseleed(strdate){
	data=$(strdate).attr("data");
	data=data.match(/^[\d]{4}\-[\d]{1,2}\-[\d]{1,2}/);
	tv=$("#checkeddatev").val();
	tv=tv.replace(","+data,"")
	$("#checkeddatev").val(tv);	
}
</script>